package erp

import (
	"context"
	"strings"

	"mall/common/errorx"
	"mall/common/globalkey"
	"mall/common/utils/pointy"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/enum/errorcode"
	"mall/service/order/rpc/orderclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeliverGoodsCallbackLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 订单发货成功回调
func NewDeliverGoodsCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeliverGoodsCallbackLogic {
	return &DeliverGoodsCallbackLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeliverGoodsCallbackLogic) DeliverGoodsCallback(
	req *types.DeliverGoodsCallbackReq,
) (*types.ErpCallbackResp, error) {
	// logrus.Infof(fmt.Sprintf("DeliverGoodsCallbackReq: %+v", req))
	//
	logx.Infof("DeliverGoodsCallback req: %+v ", req)

	if req.TradeId == nil || len(strings.TrimSpace(*req.TradeId)) == 0 {
		logx.Errorw("DeliverGoodsCallback Error: TradeId is empty")
		return nil, errorx.NewCodeError(errorcode.InvalidArgument,
			l.svcCtx.Trans.Trans(l.ctx, "common.validationError"))
	}

	if req.Waybill == nil || len(strings.TrimSpace(*req.Waybill)) == 0 {
		logx.Errorw("DeliverGoodsCallback Error: Waybill is empty")
		return nil, errorx.NewCodeError(errorcode.InvalidArgument,
			l.svcCtx.Trans.Trans(l.ctx, "common.validationError"))
	}

	if req.DeliveryCode == nil || len(strings.TrimSpace(*req.DeliveryCode)) == 0 {
		logx.Errorw("DeliverGoodsCallback Error: DeliveryCode is empty")
		return nil, errorx.NewCodeError(errorcode.InvalidArgument,
			l.svcCtx.Trans.Trans(l.ctx, "common.validationError"))
	}

	if req.DeliveryName == nil || len(strings.TrimSpace(*req.DeliveryName)) == 0 {
		logx.Errorw("DeliverGoodsCallback Error: DeliveryName is empty")
		return nil, errorx.NewCodeError(errorcode.InvalidArgument,
			l.svcCtx.Trans.Trans(l.ctx, "common.validationError"))
	}

	if req.ShopNick == nil || len(strings.TrimSpace(*req.ShopNick)) == 0 || strings.TrimSpace(*req.ShopNick) != l.svcCtx.Config.Erp.ShopNick {
		logx.Errorw("DeliverGoodsCallback Error: ShopNick is empty")
		return nil, errorx.NewCodeError(errorcode.InvalidArgument,
			l.svcCtx.Trans.Trans(l.ctx, "common.validationError"))
	}

	if len(req.Packages) > 0 {
		// Todo: 通过 Result 传递多包裹信息
	}

	var orderType *uint32 //判断订单类型  售后or正常
	orderSn := *req.TradeId
	if orderSn[:2] == "SH" {
		orderType = pointy.GetPointer(globalkey.AfterSaleOrderShipment)
	} else {
		orderType = pointy.GetPointer(globalkey.NormalOrderShipment)
	}

	_, err := l.svcCtx.OrderRpc.BackendCreateOrderShipment(l.ctx, &orderclient.OrderShipmentInfo{
		OrderSn:   req.TradeId,
		No:        req.Waybill,
		Name:      req.DeliveryName,
		Code:      req.DeliveryCode,
		OrderType: orderType,
	})
	if err != nil {
		logx.Errorw("DeliverGoodsCallback Error:", logx.Field("detail", err.Error()))
		return nil, err
	}

	return &types.ErpCallbackResp{
		Success: true,
	}, nil
}
